/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CLI;

import api.RestClientCourierImpl;
import api.RestClientCourierInteface;
import api.RestClientCustomerImpl;
import api.RestClientCustomerInterface;
import com.martiansoftware.jsap.FlaggedOption;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPException;
import com.martiansoftware.jsap.JSAPResult;
import com.martiansoftware.jsap.Switch;
import cz.muni.restinterface.CourierDTO;
import cz.muni.restinterface.CustomerDTO;

/**
 *
 * @author osiris
 */
public class ArgumentParser {

    private JSAP jsap;
    private JSAPResult config;
    private RestClientCustomerInterface rc;
    private api.RestClientCourierInteface restcourier;

    public RestClientCourierInteface getRestcourier() {
        return restcourier;
    }

    public void setRestcourier(RestClientCourierInteface restcourier) {
        this.restcourier = restcourier;
    }
    //which entity we want to manipulate
    private Switch customerSwitch; //--customer
    private Switch courierSwitch; //--courier
    //what operation we want to perform
    private Switch createSwitch; //--create
    private Switch findAllSwitch; // -- findAll
    private FlaggedOption delete; // --delete ID
    private FlaggedOption update; // --update ID
    private FlaggedOption findByFirstName; // --findbyfirstname firstName
    private FlaggedOption findByLastName; // --findbylastname lastname
    private FlaggedOption findByEmail; // --findbyemail email
    private FlaggedOption findByPhoneNumber; // --findbyphonenumber phonenumber
    private FlaggedOption findById; // --findbyid id
    //what property do we want to set / update (customer)
    private FlaggedOption firstName; // --firstname firstname
    private FlaggedOption lastName; // --lastname lastname
    private FlaggedOption email; // --email email
    private FlaggedOption phoneNumber; //-phonenumber number
    private FlaggedOption postalCode; // --postalcode postalcode
    private FlaggedOption city;
    private FlaggedOption streetNumber;
    private FlaggedOption street;
    private FlaggedOption username;

    public JSAPResult getConfig() {
        return config;
    }

    public void setConfig(JSAPResult config) {
        this.config = config;
    }

    public ArgumentParser(JSAP jsap) throws JSAPException {


        customerSwitch = new Switch("customer").setLongFlag("customer");
        courierSwitch = new Switch("courier").setLongFlag("courier");
        createSwitch = new Switch("create").setLongFlag("create");
        findAllSwitch = new Switch("findall").setLongFlag("findall");

        delete = new FlaggedOption("delete")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("delete");
        update = new FlaggedOption("update")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("update");
        findByFirstName = new FlaggedOption("findbyfirstname")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("findbyfirstname");
        findByLastName = new FlaggedOption("findbylastname")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("findbylastname");
        findByEmail = new FlaggedOption("findbyemail")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("findbyemail");
        findByPhoneNumber = new FlaggedOption("findbyphonenumber")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("findbyphonenumber");
        findById = new FlaggedOption("findbyid")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("findbyid");

        firstName = new FlaggedOption("firstname")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("firstname");
        lastName = new FlaggedOption("lastname")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("lastname");
        email = new FlaggedOption("email")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("email");
        street = new FlaggedOption("street")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("street");
        streetNumber = new FlaggedOption("stretnumber")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("stretnumber");
        city = new FlaggedOption("city")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("city");
        postalCode = new FlaggedOption("postalcode")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("postalcode");
        phoneNumber = new FlaggedOption("phonenumber")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("phonenumber");
        username = new FlaggedOption("username")
                .setStringParser(JSAP.STRING_PARSER)
                .setLongFlag("username");

        jsap.registerParameter(createSwitch);
        jsap.registerParameter(findAllSwitch);
        jsap.registerParameter(customerSwitch);
        jsap.registerParameter(courierSwitch);
        jsap.registerParameter(delete);
        jsap.registerParameter(update);
        jsap.registerParameter(findByFirstName);
        jsap.registerParameter(findByLastName);
        jsap.registerParameter(findByEmail);
        jsap.registerParameter(findByPhoneNumber);
        jsap.registerParameter(findById);
        jsap.registerParameter(firstName);
        jsap.registerParameter(lastName);
        jsap.registerParameter(email);
        jsap.registerParameter(street);
        jsap.registerParameter(streetNumber);
        jsap.registerParameter(city);
        jsap.registerParameter(postalCode);
        jsap.registerParameter(phoneNumber);
        jsap.registerParameter(username);

        this.jsap = jsap;
    }

    public JSAP getJsap() {
        return jsap;
    }

    public void setJsap(JSAP jsap) {
        this.jsap = jsap;
    }

    public RestClientCustomerInterface getRc() {
        return rc;
    }

    public void setRc(RestClientCustomerInterface rc) {
        this.rc = rc;
    }

    public void parseArguments() {

        JSAPResult cfg = this.config;
        // String output = cfg.getString("findbyfirstname");
        //  System.out.println("output: "+output);



        //--customer entity
        if (cfg.getBoolean("customer")) {
            RestClientCustomerInterface restcustomer = new RestClientCustomerImpl("http://localhost:8080/pa165/index.jsp/Rest/Customer/?");
            this.setRc(restcustomer);

            if (cfg.getBoolean("create")) {

                CustomerDTO customer = new CustomerDTO();
                customer.setCity(this.getConfig().getString("city"));
                customer.setEmail(this.getConfig().getString("email"));
                customer.setFirstName(this.getConfig().getString("firstname"));
                customer.setLastName(this.getConfig().getString("lastname"));
                customer.setPhoneNumber(this.getConfig().getString("phonenumber"));
                customer.setPostalCode(this.getConfig().getString("postalcode"));
                customer.setStreet(this.getConfig().getString("street"));
                customer.setStreetNumber(this.getConfig().getString("streetnumber"));
                this.rc.create(customer);
                System.out.println("Create call was sucessful");


            } else if (cfg.getBoolean("findall")) {

                System.out.println("Find all customers");
                this.rc.findall();
            } else if (cfg.getString("findbyfirstname") != null) {

                System.out.println("Find customer with first name: \"" + cfg.getString("findbyfirstname") + "\"");
                System.out.println("entry = " + this.rc.findByFirstName(cfg.getString("findbyfirstname")));

            } else if (cfg.getString("delete") != null) {

                this.rc.delete(cfg.getString("delete"));
                System.out.println("Delete call was sucessful");

            } else if (cfg.getString("update") != null) {

                CustomerDTO customer = new CustomerDTO();
                customer.setId(Long.valueOf(cfg.getString("update")));
                customer.setCity(this.getConfig().getString("city"));
                customer.setEmail(this.getConfig().getString("email"));
                customer.setFirstName(this.getConfig().getString("firstname"));
                customer.setLastName(this.getConfig().getString("lastname"));
                customer.setPhoneNumber(this.getConfig().getString("phonenumber"));
                customer.setPostalCode(this.getConfig().getString("postalcode"));
                customer.setStreet(this.getConfig().getString("street"));
                customer.setStreetNumber(this.getConfig().getString("streetnumber"));
                this.rc.update(customer);

                System.out.println("Update call was sucessful");
            } else if (cfg.getString("findbyid") != null) {

                System.out.println("Find customer with ID: " + cfg.getString("findbyid"));
                System.out.println("entry = " + this.rc.findById(cfg.getString("findbyid")));
            } else if (cfg.getString("findbylastname") != null) {

                System.out.println("Find customer with last name: \"" + cfg.getString("findbylastname") + "\"");
                System.out.println("entry = " + this.rc.findByLastName(cfg.getString("findbylastname")));
            } else if (cfg.getString("findbyemail") != null) {

                System.out.println("Find customer with email: \"" + cfg.getString("findbyemail") + "\"");
                System.out.println("entry = " + this.rc.findByEmail(cfg.getString("findbyemail")));
            } else if (cfg.getString("findbyphonenumber") != null) {

                System.out.println("Find customer with phone number: \"" + cfg.getString("findbyphonenumber") + "\"");
                System.out.println("entry = " + this.rc.findByPhoneNumber(cfg.getString("findbyphonenumber")));
            }

        } //--courier entity
        else if (cfg.getBoolean("courier")) {

            RestClientCourierInteface rstcr = new RestClientCourierImpl("http://localhost:8080/pa165/index.jsp/Rest/Courier/?");
            this.setRestcourier(rstcr);

            if (cfg.getBoolean("create")) {

                CourierDTO courier = new CourierDTO();
                courier.setFirstName(this.getConfig().getString("firstname"));
                courier.setLastName(this.getConfig().getString("lastname"));
                courier.setUserName(this.config.getString("username"));
                this.restcourier.create(courier);
                System.out.println("Create call was sucessful");

            } else if (cfg.getString("update") != null) {

                CourierDTO courier = new CourierDTO();
                courier.setId(Long.valueOf(this.getConfig().getString("update")));
                courier.setFirstName(this.getConfig().getString("firstname"));
                courier.setLastName(this.getConfig().getString("lastname"));
                courier.setUserName(this.config.getString("username"));
                this.restcourier.update(courier);
                System.out.println("Update call was sucessful");

            } else if (cfg.getString("findbyid") != null) {
                
                System.out.println("Find courier with ID: " + cfg.getString("findbyid"));
                System.out.println("entry = " + this.restcourier.findById(cfg.getString("findbyid")));
            } else if (cfg.getString("delete") != null) {

                this.restcourier.delete(cfg.getString("delete"));
                System.out.println("Delete call was sucessful");

            } else if (cfg.getBoolean("findall")) {

                System.out.println("Find all couriers");
                this.restcourier.findall();
            }


        } else {

            System.out.println("error while parsing arguments");
        }

    }
}
